package com.shiguiwu.springmybatis.spring.transaction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @description: 书服务
 * @author: stone
 * @date: Created by 2021/6/14 17:23
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.transaction
 */
@Service
public class BookService {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void batchInsert(String ...names) {
        int result = 0;
        Arrays.stream(names).forEach(e -> {
            int update = jdbcTemplate.update("insert INTO book(book_name) values(?)", e);
            System.out.println("==================>" + update);
        });

        delete();



    }

    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void delete() {
        int update = jdbcTemplate.update("insert INTO book(book_name) values(?)", "vue");
        throw new RuntimeException("haha");

    }

    public List<Map<String, Object>> list() {
        return jdbcTemplate.queryForList("select * from book");
    }

}
